package com.lvandroid.play.player;

import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.lvandroid.play.PlayerApplication;
import com.lvandroid.play.bean.HtmlItem;
import com.lvandroid.play.util.LogUtil;
import com.lvandroid.player.R;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.List;

/**
 * 播放本地HTML文件，或者网络url
 */
public class HTMLPlayer extends AllRoundPlayer<ViewGroup, HtmlItem> {

    private static final String TAG = "HTMLPlayer";

    protected LinearLayout mParentPanel;
    protected WebView mWebView;

    HTMLPlayer() {
        mPlayItem = new HtmlItem();
        initInternal();
    }

    private void initInternal() {
        mParentPanel = (LinearLayout) PlayerApplication.getLayoutInflater().inflate(R.layout.layout_html_player, null);
        mWebView = mParentPanel.findViewById(R.id.web_view);

        initWebView();
    }

    private void initWebView() {
        WebSettings mWebSettings = mWebView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebSettings.setAllowFileAccess(true);
        mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mWebSettings.setSupportZoom(true);
        mWebSettings.setBuiltInZoomControls(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setSupportMultipleWindows(true);
        mWebSettings.setAppCacheEnabled(true);
        mWebSettings.setDomStorageEnabled(true);
        mWebSettings.setGeolocationEnabled(true);
        mWebSettings.setAppCacheMaxSize(Long.MAX_VALUE);
        mWebSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }
        });
    }

    /**
     * 内部显示初始化
     */
    private void initDisplayInternal() {
        mDisplay.removeAllViews();
        mDisplay.addView(mParentPanel);
    }

    @Override
    public void setDisplay(ViewGroup v) {
        mDisplay = v;
        initDisplayInternal();
    }

    @Override
    public void setDataSource(String source) {
        mPlayItem.url = source;
    }

    @Override
    public void setDataSource(List<String> source) {
        mPlayItem.mUrls = source;
        if(TextUtils.isEmpty(mPlayItem.url) && null != source && source.size() > 0) {
            mPlayItem.url = source.get(0);
        }
    }

    @Override
    public void start() {
        if(null == mDisplay) {
            LogUtil.e(TAG, "Please call display method before start.");
            return;
        }

        mWebView.loadUrl(mPlayItem.url);
    }

    @Override
    public void start(String source) {
        setDataSource(source);
        start();
    }

    @Override
    public void start(List<String> source, ViewGroup display) {
        setDataSource(source);
        setDisplay(display);
        start();
    }

}
